
package com.twohalf.mifty.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.thrift.TException;

import club.jmint.mifty.dao.Sql;
import club.jmint.mifty.log.MyLog;
import club.jmint.mifty.service.MiftyService;
import com.twohalf.mifty.service.gen.SettingService.Iface;
import com.twohalf.mifty.utils.Tools;

public class SettingServiceImpl extends MiftyService implements Iface {
	private String dbName = "db_twohalf";
	private String settingTable = dbName+".t_settings";
	Sql ddl = Sql.getInstance();
	
	/**
	 * @param name(*),value,note,type,operator_id,operator_name
	 * @return errorCode
	 */
	@Override
	public Map<String, String> settingCreate(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String name = params.get("name");

		int erows = ddl.sqlCreate(settingTable, params);
		if (erows<0){
			MyLog.logger.error("setting create failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		}
		else if (erows==0){
			
		}
		else{
			MyLog.logger.info("setting created: " + name);
		}
		return resultMap;
	}

	/**
	 * @param given one or more fields
	 * @return errorCode
	 */
	@Override
	public Map<String, String> settingUpdate(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String name = params.get("name");
		String condition = name="'" + name +"'";
		
		int erows = ddl.sqlUpdate(settingTable, params, condition);
		if (erows<0){
			MyLog.logger.error("setting update failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		} else if (erows==0){
			MyLog.logger.warn("setting not exists: " + name);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		} else {
			MyLog.logger.info("udpate setting: " + name);
		}

		return resultMap;
	}

	/**
	 * @param name
	 * @return setting info fields
	 */
	@Override
	public Map<String, String> settingInfoQuery(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String name = params.get("name");
		String condition = name="'"+name+"'";
		
		String fields = params.get("fields");
		String[] fieldlist = fields.split(",");

		List<Object[]> list = ddl.sqlSelect(settingTable, fields, condition);
		//if (list==null || list.isEmpty()){
		if (list.isEmpty()){
			MyLog.logger.warn("setting not exists: " + name);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		}
		MyLog.logger.info("query setting: " + name);
		
		Object[] obj = list.get(0);
		for(int i=0;i<fieldlist.length;i++){
			resultMap.put(fieldlist[i], ddl.getSqlResultFieldAsString(obj[i]));
		}
		return resultMap;
	}

	@Override
	public Map<String, String> settingListQuery(Map<String, String> params) throws TException {
		// TODO Auto-generated method stub
		return null;
	}

}

